package bluerain.io.adapter.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import bluerain.io.adapter.R;

/**
 * Created by foredawn on 15-9-11.
 * ExpandableList基本用法
 */
public class ExpandableListViewTest extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_list_view_test);
        ExpandableListView listView = (ExpandableListView) findViewById(R.id.expand_list);
        ExpandableListAdapter adapter = new BaseExpandableListAdapter() {

            private String[] armTypes = new String[]{
                    "Java书籍",
                    "Android书籍",
                    "NodeJs教程"
            };
            private String[][] arms = new String[][]{
                    {"深入理解java.7.核心技术与最佳实践", "java与模式", "重构-改善既有代码的设计"},
                    {"疯狂android讲义", "android 4高级编程", "深入理解安卓"},
                    {"javascript高级编程", "node.js入门经典", "npm_js"}
            };
            private int[] logos = new int[]{
                    R.drawable.li_bai,
                    R.drawable.wang_cai,
                    R.drawable.xiao_ming
            };

            @Override
            public int getGroupCount() {
                return armTypes.length;
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                return arms[groupPosition].length;
            }

            @Override
            public Object getGroup(int groupPosition) {
                return armTypes[groupPosition];
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                return arms[groupPosition][childPosition];
            }

            @Override
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }

            @Override
            public long getChildId(int groupPosition, int childPosition) {
                return childPosition;
            }

            @Override
            public boolean hasStableIds() {
                return true;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                LinearLayout linear = new LinearLayout(ExpandableListViewTest.this);
                linear.setOrientation(LinearLayout.HORIZONTAL);
                ImageView logo = new ImageView(ExpandableListViewTest.this);
                logo.setImageResource(logos[groupPosition]);
                logo.setLayoutParams(new LinearLayout.LayoutParams(150, 150));
                TextView text = getTextView();
                text.setText(armTypes[groupPosition]);
                linear.addView(logo);
                linear.addView(text);
                return linear;
            }

            private TextView getTextView() {
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                TextView text = new TextView(ExpandableListViewTest.this);
                text.setLayoutParams(lp);
                text.setTextSize(20);
                text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                text.setPadding(36, 0, 0, 0);
                return text;
            }

            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                TextView text = getTextView();
                text.setText(arms[groupPosition][childPosition]);
                return text;
            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return true;
            }
        };
        listView.setAdapter(adapter);
    }
}
